import {
    WebSocketGateway,
    SubscribeMessage,
    MessageBody,
    OnGatewayInit,
    OnGatewayConnection,
    OnGatewayDisconnect,
} from '@nestjs/websockets';
import { Server, Socket } from 'socket.io';
@WebSocketGateway({
    cors: {
        origin: 'http://localhost:5173', // 前端地址
    },
})
export class SocketGateway
    implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
    private server: Server;
    afterInit(server: Server) {
        this.server = server;
    }
    handleConnection(client: Socket) {
        console.log('Client connected:', client.id);
    }
    handleDisconnect(client: Socket) {
        console.log('Client disconnected:', client.id);
    }
    @SubscribeMessage('message')
    handleMessage(@MessageBody() data: string): void {
        this.server.emit('message', data); // 广播消息给所有客户端
    }
}